#!/bin/bash
# 描述: 主功能函数所依赖的功能函数
# 版本: v0.1
# 日期: 20230725
# 作者: 肖晨光


expect_cmd='/usr/bin/expect'

# expect环境部署
expect_install(){
# 判断expect是否安装
 if [ -f "${expect_cmd}"  ];then
   echo -e "\033[31mexpect环境已经部署完毕\033[0m"
 else
   # 安装expect
   read -p "请输入您准备以哪种方式安装expect(online|offline): " install_type
   install_type=${install_type:-online}
   if [ "${install_type}" == "online" ];then
      # 指定安装命令
      "${cmd_type}" -y install expect >> /dev/null 2>&1 && echo -e "\e[32mexpect软件安装成功\e[0m" ||
       ("\e[32mexpect软件安装失败\e[0m")
   else
      echo "离线安装"
   fi

 fi
}

# ssh密钥生成
sshkey_create(){
   current_user=$(whoami)
   [ "${current_user}" == "root" ] && user_id='/root' || user_id="/home/${current_user}"
   [ -d "${user_id}/.ssh" ] && rm -rf "${user_id}/.ssh"
   # 生成ssh密钥对
   ssh-keygen -t rsa -P "" -f "${user_id}/.ssh/id_rsa" && echo -e "\e[32mssh密钥对创建成功\e[0m" ||
       ("\e[32mssh密钥对生成失败\e[0m")
}

# hosts文件生成
hosts_create(){
  awk '$0~"k8s-"{print $1,$2".x.com",$2}' "${con_dir}/${host_name}"  >> "${host_file}"
  echo -e "\e[32mhosts生成完毕\e[0m" 
}


# ssh跨主机免密认证
sshkey_auth_func(){
  # 接收参数
    local ip_list="$*"
  #执行跨主机免密操作
    local cmd="ssh-copy-id -i $HOME/.ssh/id_rsa.pub"
    for ip in ${ip_list} ;do
      local ip_target="${login_user}@${ip}"
#     local host=$(grep $ip ${con_dir}/${host_name} | awk '(print $2)')
#     local host_target="${login_user}@${host}"
      expect_autoauth_func ${cmd} ${ip_target} 
#     ${cmd} ${host_target}
    done
}


# expect实现自动化命令执行
expect_autoauth_func(){
# 接收参数
    local remote_cmd="$*"
    #expect全程干预
    /usr/bin/expect -c"
      spawn ${remote_cmd}
      expect {
        \"yes/no\" {send \"yes\r\"; exp_continue}
        \"*password*\" {send \"${login_pass}\r\"; exp_continue}
        \"*password*\" {send \"${login_pass}\r\";}
      }"

}

#scp_file(){
#接收外部的参数
#   local ip_list="$*"
#   for ip in ${ip_list};do
#       scp "${host_file}" "${login_user}@${ip}:${host_file} "
#   done
#}

scp_file(){
#接收外部的参数
    local target_dir=$(echo $* | awk '{print $NF}')
    local source_file=$(echo $* | awk '{print $(NF-1)}')
    local ip_list=$(echo $* | awk '{$NF=null;$(NF-1)=null;print $0}')
    local filename=$(basename ${source_file})
    local target_file="${target_dir}/${filename}"
    for ip in ${ip_list};do
    scp "${source_file}" "${login_user}@${ip}:${target_file} "
    done
}

# 跨主机设定主机名
set_hostname(){
    local ip_list="$*"
    for ip in ${ip_list};do
      local remote_host="${login_user}@${ip}"
      local hostname=$(grep ${ip} ${host_file} | awk '{print $NF}')
      ssh ${remote_host} "hostnamectl set-hostname ${hostname}"
    done
}

# 生成ip列表
create_ip_list(){
  #接收参数
    local  ip_net="$1"
    local  ip_tail="$2"
    local  ip_list=""
  #生成ip列表
  for i in $(eval echo ${ip_tail});do
    ip_addr=$(echo -n "${ip_net}.${i} ")
    ip_list="${ip_list}${ip_addr}"  
  done
  echo "${ip_list}"
}

# 软件源同步
repo_update(){
    #接收参数
    local ip_list="$*"
    if [ "${os_type}" == "centos" ];then
        update_cmd="${cmd_type} makecache fast"
    else
        update_cmd="${cmd_type} update -y"
    fi
    for ip in ${ip_list};do
        ssh "${login_user}@${ip}" "${update_cmd}"
    	echo -e "\033[32m主机${ip}软件源更新完毕!\033[0m"
	done
}
